<template>
  <div class="carousel__wrap" @mouseenter="toggleClass" @mouseleave="toggleClass($event, 'delete')">
    <span class="carousel__arrow--left">
      <i class="el-icon-arrow-left"></i>
    </span>
    <div class="carousel__container" ref="carousel__container">
      <slot></slot>
    </div>
    <span class="carousel__arrow--right" @click="a">
      <i class="el-icon-arrow-right"></i>
    </span>
  </div>
</template>

<script>
export default {
  methods: {
    toggleClass(ev, type) {
      const itemNodes = this.$refs["carousel__container"].childNodes
      if ((itemNodes[0].offsetWidth + 40) * itemNodes.length - 40 < this.$refs["carousel__container"].offsetWidth) return false;
      if (type !== "delete")
        return (ev.target.className = `${ev.target.className} active`);
      ev.target.className = ev.target.className.replace("active", "");
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/mixin.scss";
.carousel__wrap {
  margin-top: 40px;
  width: 100%;
  position: relative;
  &.active {
    .carousel__arrow--left,
    .carousel__arrow--right {
      z-index: 1;
      opacity: 1;
    }
  }
  .carousel__arrow--left,
  .carousel__arrow--right {
    z-index: -1;
    transition: all 0.5s ease;
    opacity: 0;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 64px;
    height: 120px;
    background: #efeff5;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.5s ease;
    &:hover {
      @include boxShadow;
    }
    i {
      transition: all 0.5s ease;
      font-size: 40px;
      color: #b6b9c5;
    }
  }
  .carousel__arrow--left {
    left: 0;
    border-radius: 0 5px 0 5px;
  }
  .carousel__arrow--right {
    right: 0;
    border-radius: 5px 0 5px 0;
  }
  .carousel__container {
    transition: all .5s ease;
    display: flex;
    white-space: nowrap;
    overflow: hidden;
    /deep/ .carousel__item {
      margin-right: 40px;
      &:last-child {
        margin-left: 0px;
      }
    }
    .item {
      margin-right: 40px;
      &:last-child {
        margin: 0;
      }
    }
    .tree {
      flex-basis: 440px;
      flex-shrink: 0;
    }
  }
}
</style>
